#include "gtest/gtest.h" //include to use Google Unit test's stuff

#include <iostream>
#include <string>

using namespace std;

int argc=2;
char *argv[]={"TextBuddy","mytextfile.txt"};

/************************************************************************/

/* We write test cases here */

/************************************************************************/


TEST(basic_test, addTest)
{
	TextBuddy::clear(argv);
	string result = TextBuddy::add("This is fun",argv);
	ASSERT_EQ("added to mytextfile.txt: \"This is fun\"\n",result);
	TextBuddy::clear(argv);
}

TEST(basic_test, clearTest)
{
	string result_add = TextBuddy::add("This is fun",argv);
	string result_clear = TextBuddy::clear(argv);
	ASSERT_EQ("all content deleted from mytextfile.txt\n",result_clear);

	//check if it was actually cleard
	vector<string> fileContent = TextBuddy::getFileContent(argv);
	ASSERT_EQ(0,fileContent.size());

}

TEST(basic_test, extractFirstWordTest)
{
	string result = TextBuddy::extractFirstWord("This is fun");
	ASSERT_EQ("This",result);
}

TEST(basic_test, extractAfterFirstWordTest)
{
	string firstWord = TextBuddy::extractFirstWord("This is fun");
	string result = TextBuddy::extractAfterFirstWord("This is fun",firstWord);
	ASSERT_EQ("is fun",result);
}

TEST(basic_test, throwErrorMessageTest)
{
	string result = TextBuddy::throwErrorMessage("This is fun");
	ASSERT_EQ("This is fun\n",result);
}

TEST(basic_test, getFileContent_and_display_test){

	TextBuddy::add("This is fun",argv);
	TextBuddy::add("This is fun!",argv);
	TextBuddy::add("This is fun!!!",argv);
	vector<string> result = TextBuddy::getFileContent(argv);
	ASSERT_EQ("This is fun",result[0]);
	ASSERT_EQ("This is fun!",result[1]);
	ASSERT_EQ("This is fun!!!",result[2]);

	string display_result = TextBuddy::display(argv);
	ASSERT_EQ("1. This is fun\n2. This is fun!\n3. This is fun!!!\n",display_result);
	TextBuddy::clear(argv);
}

TEST(basic_test, getFileContent_and_display_test_FILE_EMPTY){

	string display_result = TextBuddy::display(argv);
	ASSERT_EQ("mytextfile.txt is empty\n",display_result);

}

TEST(basic_test, sortFileContentTest){

	//Test 1
	TextBuddy::add("zzzz",argv);
	TextBuddy::add("bbbb",argv);
	TextBuddy::add("aaaa",argv);
	string result_sort = TextBuddy::sortFileContent(argv);
	ASSERT_EQ("File sorted!",result_sort);

	string result_display = TextBuddy::display(argv);
	ASSERT_EQ("1. aaaa\n2. bbbb\n3. zzzz\n",result_display);
	TextBuddy::clear(argv);

	//Test 2 with different file content
	TextBuddy::add("rhggdh",argv);
	TextBuddy::add("jsadjaj",argv);
	TextBuddy::add("abcdef",argv);
	result_sort = TextBuddy::sortFileContent(argv);
	ASSERT_EQ("File sorted!",result_sort);

	result_display = TextBuddy::display(argv);
	ASSERT_EQ("1. abcdef\n2. jsadjaj\n3. rhggdh\n",result_display);
	TextBuddy::clear(argv);
}

TEST(basic_test, sortFileContentTest_EMPTY_FILE){

	string result = TextBuddy::sortFileContent(argv);
	ASSERT_EQ("Cannot sort an empty file!",result);
}

TEST(basic_test, searchWordTest){

	TextBuddy::add("This is",argv);
	TextBuddy::add("This is fun",argv);
	TextBuddy::add("This is!",argv);

	string result = TextBuddy::searchWord("fun",argv);
	ASSERT_EQ("1 result found.\nLine with the keyword -\n2. This is fun\n",result);
	TextBuddy::clear(argv);
}

//This unit test also takes care substring search. Substrings should not be found in search
//Only words should be found.
TEST(basic_test, searchWordTest_multipleInstances){

	TextBuddy::add("This",argv);
	TextBuddy::add("This is fun",argv);
	TextBuddy::add("fun is",argv);

	string result = TextBuddy::searchWord("is",argv);
	ASSERT_EQ("2 results found.\nLines with the keyword -\n2. This is fun\n3. fun is\n",result);
	TextBuddy::clear(argv);
}

TEST(basic_test, searchWordTest_noResultFound){

	TextBuddy::add("This",argv);
	TextBuddy::add("This is fun",argv);
	TextBuddy::add("fun is",argv);

	string result = TextBuddy::searchWord("name",argv);
	ASSERT_EQ("No results found!",result);
	TextBuddy::clear(argv);
}

void runTest(int argument_count, char** argument_vars)

{
	testing::InitGoogleTest(&argument_count, argument_vars); //initialize GTest

	RUN_ALL_TESTS();

	std::getchar(); //pause the program after all the testing

}

